<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>节流</title>
</head>

<body>
  定时器方法
  <input type="text" id="ipt" placeholder="输入内容">
  <script>
    
    // 目的是减小请求频次，特点是每隔n秒发起一次请求，均匀发起
    let ipt = document.querySelector('#ipt')
    function read() {
     console.log(this.value);
    }
    function throttle(fn, wait) {
      let timeout
      return function () {
        let val = this
        let args = arguments

        if (!timeout) {
          timeout = setTimeout(()=>{
            timeout = null
            fn.apply(val,args)
          },wait)
        }
      }

    }
    ipt.oninput = throttle(read, 1000)
  </script>
</body>

</html>